﻿using NHibernate;
using NHibernate.Cfg;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace chat_app.NhibernateLogic.Managers
{
    public class SessionManager : IDisposable
    {
        public static ISessionFactory SessionFactory;
        public ISession CurrentSession
        {
            get { return (ISession)HttpContext.Current.Items["current.session"]; }
            set { HttpContext.Current.Items["current.session"] = value; }
        }


        public SessionManager()
        {
            if (SessionFactory == null)
                SessionFactory = CreateSessionFactory();
            CurrentSession = SessionFactory.OpenSession();
        }

        protected static ISessionFactory CreateSessionFactory()
        {
            return new Configuration().Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.config")).BuildSessionFactory();
        }

        public void Dispose()
        {
            if (CurrentSession != null)
            {
                CurrentSession.Flush();
                CurrentSession.Dispose();
            }
        }
    }
}